package com.crazymaker.cloud.nacos.demo.config.controller;

import com.crazymaker.cloud.nacos.demo.config.DataSourceProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/config")
@Api(tags = "Nacos 配置中心演示")
public class ConfigGetController {


    @Value("${foo.bar:empty}")
    private String bar;


    @Value("${spring.datasource.username:empty}")
    private String dbusername;


    @Value("${some.proper:empty}")
    private String proper;

    @Value("${sharding.datasource.username:empty}")
    private String username;

    //@Value("${sharding.datasource.multi-conn:empty}")
    //private List<DataSourceProperties.MultiConn> multiConn = new ArrayList<>();

    private DataSourceProperties dataSourceProperties;

    @Autowired
    public void setDataSourceProperties(DataSourceProperties dataSourceProperties) {
        this.dataSourceProperties = dataSourceProperties;
    }

    //获取配置的内容
    @ApiOperation(value = "获取配置的内容")
    @RequestMapping(value = "/bar", method = RequestMethod.GET)
    public String getBar() {
        return "bar is :" + bar;
    }

    //获取配置的内容
    @ApiOperation(value = "获取配置的db username")
    @RequestMapping(value = "/dbusername", method = RequestMethod.GET)
    public String getDbusername() {
        return "db username is :" + dbusername;
    }


    //获取独立配置的内容
    @ApiOperation(value = "获取独立配置 proper")
    @RequestMapping(value = "/proper", method = RequestMethod.GET)
    public String getProper() {
        return "some.proper is :" + proper;
    }


    //获取shardingJDBC配置的内容1
    @ApiOperation(value = "获取shardingJDBC配置的内容1")
    @RequestMapping(value = "/shardingJDBCUsername", method = RequestMethod.GET)
    public String getShardingJDBCUsername() {
        return "userName is :" + username;
    }

    ////获取shardingJDBC配置的内容2
    //@ApiOperation(value = "获取shardingJDBC配置的内容2")
    //@RequestMapping(value = "/shardingJDBCUsername2", method = RequestMethod.GET)
    //public String getShardingJDBCUsername2() {
    //    return "multiConn is :" + multiConn;
    //}

    //获取shardingJDBC配置的内容3
    @ApiOperation(value = "获取shardingJDBC配置的内容3")
    @RequestMapping(value = "/shardingJDBCUsername3", method = RequestMethod.GET)
    public String getShardingJDBCUsername3() {
        System.out.println(dataSourceProperties.getUsername());
        System.out.println(dataSourceProperties.getPassword());
        for (int i = 0; i < dataSourceProperties.getMultiConn().size(); i++) {
            DataSourceProperties.MultiConn multiConn = dataSourceProperties.getMultiConn().get(i);
            System.out.println(multiConn.getUrl());
            System.out.println(multiConn.getUsername());
            System.out.println(multiConn.getPassword());
        }
        System.out.println(dataSourceProperties.getDruid().getConnectionProperties());

        return "dataSourceProperties is :" + dataSourceProperties;
    }

}
